var access = require('../access');
var util = require('util');

app.get('/users', access.admin, function(request, response) {
	model.User.find().sort('lastName', 1).run(function(e, users) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else {
			response.render('Users/list.jade', {
				title : 'User Managment',
				users : users,
				subset : 'All Users',
				pretty : true
			});
		}
	});
});

app.get('/rushees', access.member, function(request, response) {
	model.User.find({
		type : 'Rushee'
	}).sort('lastName', 1).run(function(e, users) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else {
			response.render('Users/list.jade', {
				title : 'User Managment',
				users : users,
				subset : 'Rushees',
				pretty : true
			});
		}
	});
});

app.get('/users/:id', access.member, function(request, response) {
	model.User.findOne({
		_id : request.params.id
	}).populate('events').populate('comments.who').run(function(e, user) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else if (user) {
			response.render('Users/view.jade', {
				title : 'User Profile',
				profile : user,
				pretty : true
			});
		} else {
			res.redirect('/users');
		}
	});
});

app.get('/users/:id/edit', access.user, function(request, response) {
	model.User.findOne({
		_id : request.params.id
	}, function(e, user) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else if (user) {
			response.render('Users/edit.jade', {
				title : 'Edit Profile',
				profile : user,
				pretty : true
			});
		} else {
			res.redirect('/users');
		}
	});
});

app.get('/users/:id/comment', access.member, function(request, response) {
	model.User.findOne({
		_id : request.params.id,
		type : 'Rushee'
	}, function(e, user) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else if (user) {
			response.render('Users/comment.jade', {
				title : 'Create Comment',
				profile : user,
				pretty : true
			});
		} else {
			res.redirect('/users/' + request.params.id);
		}
	});
});

app.get('/users/:id/remove', access.user, function(request, response) {
	model.User.remove({
		_id : request.params.id
	}, function(e, user) {
		if (e) {
			response.render('error.jade', {
				title : 'User Error',
				error : e,
				pretty : true
			});
		} else {
			response.render('message.jade', {
				title : 'User Removed',
				message : 'The user has been removed',
				next : '/users/',
				pretty : true
			});
		}
	});
});

app.post('/users', access.user, function(request, response) {
	if (typeof (request.body.action) == 'string') {
		switch (request.body.action) {
		case 'comment':
			util.log('It\'s a comment');
			model.User.findOne({
				_id : request.body.id,
				type : 'Rushee'
			}, function(e, rushee) {
				if (e) {
					response.render('error.jade', {
						title : 'Update Error',
						error : e,
						pretty : true
					});
				} else if(rushee) {
					var newComment = new model.Comment();
					newComment.who = request.body.who;
					newComment.what = request.body.what;
					rushee.comments.push(newComment);
					rushee.save(function(e) {
						response.redirect('/users/' + request.body.id);
					});
				} else {
					response.render('error.jade', {
						title : 'Comment Error',
						error : 'Unknown User ID',
						pretty : true
					});
				}
			});
			break;
		case 'update':
			// Keep non-admins from changing user types
			var newType = request.user.type;
			if (/Administrator/i.test(request.user.type)) {
// util.log('Update User: type = ' + request.body.type);
				newType = request.body.type;
			}

			model.User.update({
				_id : request.body.id
			}, {
				firstName : request.body.firstName,
				lastName : request.body.lastName,
				email : request.body.email,
				type : newType
			}, function(e) {
				if (e) {
					response.render('error.jade', {
						title : 'Update Error',
						error : e,
						pretty : true
					});
				} else {
					response.render('message.jade', {
						title : 'Profile Updated',
						message : 'Your profile has been successfully updated',
						next : '/users/' + request.body.id,
						pretty : true
					});
				}
			});

			break;
		default:
			response.render('error.jade', {
				title : 'Input Error',
				error : 'The action defined for for the request was invalid!',
				pretty : true
			});
		}
	} else {
		response.render('error.jade', {
			title : 'Input Error',
			error : 'No action was defined for the request!',
			pretty : true
		});
	}
});
